package com.iscc.fake.dao.Impl;

import com.iscc.fake.domain.Address;
import com.iscc.fake.domain.Carrt;
import com.iscc.fake.dao.CartDao;
import com.iscc.fake.util.DataSourceUtile;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    @Override
    public int addCart(Carrt carrt) {
        String sql = "INSERT INTO cart(uno,pno,price,count) VALUES(?,?,?,?)";
        int result = -1;
        Connection conn=null;
        PreparedStatement psmt=null;
        ResultSet rs=null;
        try {
            conn = DataSourceUtile.getConnection();
            psmt = conn.prepareStatement(sql);

            psmt.setInt(1,carrt.getUno());
            psmt.setInt(2,carrt.getPno());
            psmt.setDouble(3,carrt.getPrice());
            psmt.setInt(4,carrt.getCount());

            result = psmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DataSourceUtile.closeReource(psmt,conn);
        }

        return result;
    }

    @Override
    public int delCart(int cartno) {
        String sql = "DELETE FROM cart WHERE cartno = ?";
        int result = -1;
        try {
            Connection conn = DataSourceUtile.getConnection();
            PreparedStatement psmt = conn.prepareStatement(sql);
            psmt.setInt(1,cartno);
            result = psmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<Carrt> selectCart() {
        List<Carrt> list=new ArrayList<>();
        String sql="SELECT c.* ,pname FROM cart c,product p WHERE c.pno=p.pno";

        Connection conn= null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn = DataSourceUtile.getConnection();

            //预处理对象
            ps =conn.prepareStatement(sql);
            //执行SQL语句，查询-executeQuery，增删改-executeUpdate():返回int:影响行数。
            rs = ps.executeQuery();
            while (rs.next()) {
                Carrt carrt=new Carrt();
                carrt.setCartno(rs.getInt(1));
                carrt.setUno(rs.getInt(2));
                carrt.setPno(rs.getInt(3));
                carrt.setPrice(rs.getDouble(6));
                carrt.setCount(rs.getInt(5));
                carrt.setPname(rs.getString(7));
                list.add(carrt);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DataSourceUtile.closeReource(rs,ps,conn);
        }
        return list;
    }

    @Override
    public int delAllCart() {
        CartDaoImpl cartDaoImpl=new CartDaoImpl();


        String sql="SELECT * from cart";

        Connection conn= null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn = DataSourceUtile.getConnection();

            //预处理对象
            ps =conn.prepareStatement(sql);
            //执行SQL语句，查询-executeQuery，增删改-executeUpdate():返回int:影响行数。
            rs = ps.executeQuery();
            while (rs.next()) {
                rs.getInt(1);
                cartDaoImpl.delCart(rs.getInt(1));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DataSourceUtile.closeReource(rs,ps,conn);
        }
        return 1;


    }

    @Override
    public double selectSum() {
        String sql="SELECT SUM(price * count) FROM cart";

        Connection conn= null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        double result=1.1;
        try {
            conn = DataSourceUtile.getConnection();

            //预处理对象
            ps =conn.prepareStatement(sql);
            //执行SQL语句，查询-executeQuery，增删改-executeUpdate():返回int:影响行数。
            rs = ps.executeQuery();
            while (rs.next()) {
                result=rs.getDouble(1);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DataSourceUtile.closeReource(rs,ps,conn);
        }
        return result;
    }

    @Override
    public Carrt seletlogo(int uno) {
        String sql="SELECT SUM(COUNT),SUM(price)FROM cart WHERE uno=?";
        ResultSet result =null;
        PreparedStatement psmt=null;
        Connection conn=null;
        Carrt carrt =null;
        try {
            carrt =new Carrt();
            conn = DataSourceUtile.getConnection();
            psmt = conn.prepareStatement(sql);
            psmt.setInt(1,uno);
            result = psmt.executeQuery();
            while (result.next()) {
                carrt.setCount(result.getInt(1));
                carrt.setPrice(result.getDouble(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DataSourceUtile.closeReource(result,psmt,conn);
        }
        return  carrt;
        }

    }
